package com.his.benchwork.service.Imp;

import com.his.benchwork.dao.BenchConfigMapper;
import com.his.benchwork.entity.BenchConfig;
import com.his.benchwork.entity.BenchConfigInfo;
import com.his.benchwork.service.BenchConfigService;
import com.his.benchwork.vo.BenchConfigVo;
import com.his.benchwork.vo.GetConfigVo;
import com.his.common.util.XMLUtil;
import com.his.login.entity.DictEmployee;
import com.xiao.common.util.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author jzy
 * @Date 2020/3/11 11:33
 * @Version 1.0
 */
@Service
public class BenchConfigServiceImpl implements BenchConfigService {

    @Resource
    private BenchConfigMapper benchConfigMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BenchConfig selectByPrimaryKey(BenchConfigVo benchConfigVo) {
        BenchConfig benchConfig = BeanUtil.copyBean(benchConfigVo,BenchConfig.class);
        return benchConfigMapper.config(benchConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BenchConfigInfo> selectBench_configList_all(DictEmployee dictEmployee) {
        return benchConfigMapper.config_list_all();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BenchConfigInfo> selectBench_configList(BenchConfigVo bench_ConfigVo) {
        BenchConfig bench_Config = BeanUtil.copyBean(bench_ConfigVo,BenchConfig.class);
        return benchConfigMapper.config_list(bench_Config);
    }

    @Override
    public List<BenchConfig> selectBench_config_parm() {
        return benchConfigMapper.config_parm();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBench_Config( BenchConfigVo bench_ConfigVo) {
        BenchConfig bench_Config = BeanUtil.copyBean(bench_ConfigVo,BenchConfig.class);
        benchConfigMapper.save_config(bench_Config);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBench_Config(BenchConfigVo bench_ConfigVo) {
        BenchConfig bench_Config = BeanUtil.copyBean(bench_ConfigVo,BenchConfig.class);
        benchConfigMapper.update_config(bench_Config);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByPrimaryKey(String configId) {
        benchConfigMapper.delete_config(configId);
    }

    @Override
    public List<Map> getBenchConfig(DictEmployee dictEmployee, GetConfigVo getConfigVo) {
        BenchConfig benchConfig = BeanUtil.copyBean(getConfigVo,BenchConfig.class);
        BenchConfig benchConfig1 =benchConfigMapper.config(benchConfig);
        if(4==benchConfig1.getScopetype()|| 5==benchConfig1.getScopetype()) {
            String sql = benchConfig1.getScopelist();
            if(!StringUtils.isEmpty(sql)) {
                sql = sql.replace("##", dictEmployee.getHOSPID());
                return benchConfigMapper.getBenchConfig(sql);
            }
        }else if(1==benchConfig1.getScopetype()|| 2==benchConfig1.getScopetype() || 3==benchConfig1.getScopetype()){
            return XMLUtil.parse(benchConfig1.getScopelist());
        }
        return null;
    }
}
